package com.molyfun.parents.activity.course.view;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.attachment.AttachmentViewer;
import com.molyfun.parents.activity.course.CourseFeedbackEditActivity;
import com.molyfun.parents.fragment.dialog.CommonEnsureDialogFragment;
import com.molyfun.parents.fragment.dialog.CourseFeedbackDialogFragment;
import com.molyfun.parents.rest.bean.CourseFeedbackListResponse;
import com.molyfun.parents.rest.bean.CourseListResponse;
import com.molyfun.parents.utils.NetWorkUtil;
import com.molyfun.parents.utils.TimeUtils;
import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.carbs.android.expandabletextview.library.ExpandableTextView;
import de.hdodenhof.circleimageview.CircleImageView;

import static cn.carbs.android.expandabletextview.library.ExpandableTextView.STATE_SHRINK;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class CourseFeedbackCellView extends LinearLayout implements View.OnClickListener {

    protected LayoutInflater layoutInflater;

    private Context context;
    private boolean inited;

    @InjectView(R.id.feedback_cell_avator)
    CircleImageView mFeedbackAvator;
    @InjectView(R.id.edit_feedback_btn)
    TextView mFeedbackBtn;
    @InjectView(R.id.feedback_user_name)
    TextView mFeedbackUserName;
    @InjectView(R.id.feedback_time)
    TextView mFeedbackTime;
    @InjectView(R.id.feedback_content)
    ExpandableTextView mFeedbackContent;
    @InjectView(R.id.feedback_attachment)
    AttachmentViewer mFeedbackAttachment;
    @InjectView(R.id.read_state)
    ImageView mReadState;

    FragmentManager mFragmentManager;

    CourseFeedbackListResponse.CourseFeedbackBean mCourseFeedbackBean;
    CourseListResponse.CourseBean mCourseBean;


    public CourseFeedbackCellView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public CourseFeedbackCellView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CourseFeedbackCellView(Context context) {
        super(context);
        init(context, null);
    }


    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_course_feedback_cell, this);
        ButterKnife.inject(this);


    }

    public void initData(CourseFeedbackListResponse.CourseFeedbackBean courseFeedbackBean, CourseListResponse.CourseBean courseBean, FragmentManager fragmentManager) {

        if (courseFeedbackBean == null) {
            return;
        }

        this.mFragmentManager = fragmentManager;
        mCourseFeedbackBean = courseFeedbackBean;
        mCourseBean = courseBean;
        mFeedbackTime.setText(TimeUtils.getDateByTimeMillis(mCourseFeedbackBean.getCreatedatetime()));
        mFeedbackUserName.setText(mCourseFeedbackBean.getUsername());
        mFeedbackContent.updateForRecyclerView(mCourseFeedbackBean.getContent(), mFeedbackContent.getWidth(), STATE_SHRINK);
        mFeedbackBtn.setOnClickListener(this);
        if (!TextUtils.isEmpty(mCourseFeedbackBean.getUserimgurl())) {
            Picasso.with(getContext())
                    .load(mCourseFeedbackBean.getUserimgurl())
                    .into(mFeedbackAvator);
        } else {
            mFeedbackAvator.setImageResource(R.mipmap.ic_header_default);
        }
        if (mCourseFeedbackBean.getUserid() != null && mCourseFeedbackBean.getUserid().equals(MFPApplication.getInstance().getUser().getId())) {
            mFeedbackBtn.setVisibility(View.VISIBLE);
        } else {
            mFeedbackBtn.setVisibility(View.GONE);
        }
        if (courseFeedbackBean.getMediaobjectlist() != null && courseFeedbackBean.getMediaobjectlist().size() > 0) {
            mFeedbackAttachment.setVisibility(View.VISIBLE);
            mFeedbackAttachment.initData(courseFeedbackBean.getMediaobjectlist());
        } else {
            mFeedbackAttachment.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(courseFeedbackBean.getStatus()) || courseFeedbackBean.getStatus().equals("0")) {
            mReadState.setVisibility(View.VISIBLE);
            mReadState.setImageResource(R.mipmap.icon_unread);
        } else {
            mReadState.setVisibility(View.VISIBLE);
            mReadState.setImageResource(R.mipmap.icon_read);
        }

    }

    private CourseFeedbackDialogFragment mCourseFeedbackDialogFragment;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.edit_feedback_btn:
                if (!NetWorkUtil.isNetworkAvailable(context)) {
                    new CommonEnsureDialogFragment.CommonEnsureDialogBuilder("没有网络，请检查网络")
                            .build(mFragmentManager.beginTransaction());
                    return;
                }

                CourseFeedbackEditActivity.startActivity(getContext(), mCourseBean.getId(), mCourseBean.getDkey(), mCourseFeedbackBean);

//                mCourseFeedbackDialogFragment = new CourseFeedbackDialogFragment.CourseFeedbackDialogBuilder(mCourseBean.getId(), mCourseBean.getDkey(), mCourseFeedbackBean)
//                        .setCourseFeedbackCallback(new CourseFeedbackDialogFragment.CourseFeedbackCallback() {
//                            @Override
//                            public void onFeedbackSuccess() {
//                            }
//
//                            @Override
//                            public void onFeedbackUpdateSuccess(CourseFeedbackEditRequest courseFeedbackEditRequest) {
//                                if (courseFeedbackEditRequest != null) {
//                                    mFeedbackTime.setText(TimeUtils.getQueryDate());
//                                    mFeedbackContent.setText(courseFeedbackEditRequest.getContent());
//                                    if (courseFeedbackEditRequest.getMediaobjectlist() != null && courseFeedbackEditRequest.getMediaobjectlist().size() > 0) {
//                                        mFeedbackAttachment.setVisibility(View.VISIBLE);
//                                        mFeedbackAttachment.initData(courseFeedbackEditRequest.getMediaobjectlist());
//                                    } else {
//                                        mFeedbackAttachment.setVisibility(View.GONE);
//                                    }
//                                    mCourseFeedbackBean.setContent(courseFeedbackEditRequest.getContent());
//                                    mCourseFeedbackBean.setMediaobjectlist(courseFeedbackEditRequest.getMediaobjectlist());
//                                }
//                            }
//                        })
//                        .build(mFragmentManager.beginTransaction());
//                CourseFeedbackListActivity.setCourseFeedbackDialogFragment(mCourseFeedbackDialogFragment);
//                if (mCourseFeedbackBean != null) {
//                    if(mCourseFeedbackBean.getFeedbacktimes() < 5){
//                        new CourseFeedbackDialogFragment.CourseFeedbackDialogBuilder(mCourseBean.getId(), mCourseBean.getDkey())
//                                .setCourseFeedbackCallback(new CourseFeedbackDialogFragment.CourseFeedbackCallback() {
//                                    @Override
//                                    public void onFeedbackSuccess() {
//                                        mCourseFeedback.setTextColor(ContextCompat.getColor(context, R.color.color_base));
//                                        mCourseBean.setFeedbacktimes(mCourseBean.getFeedbacktimes() + 1);
//                                    }
//                                })
//                                .build(mFragmentManager.beginTransaction());
//                    }else{
//                        new CommonDialogFragment.CommonDialogBuilder("最多五条课程反馈，已达到上限")
//                                .build(mFragmentManager.beginTransaction());
//                    }
//
//                }
                break;


        }
    }


}